<?php
/**
 * A simple description for this script
 *
 * PHP Version 5.2.0 or Upper version
 *
 * @package    Paylog
 * @author     Hidehito NOZAWA aka Suin <http://suin.asia>
 * @copyright  2010 Hidehito NOZAWA
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 */

class Paylog_Controller_EditBook extends Paylog_Abstract_Controller
{
	protected $bookHand = null;
	protected $bookObj  = null;
	protected $relBookUserHand = null;
	protected $bookId   = null;
	protected $input    = array();
	protected $errors   = array();

	public function __construct()
	{
		parent::__construct();
		$this->bookHand = new Paylog_Object_BookHandler;
		$this->relBookUserHand = new Paylog_Object_RelBookUserHandler;
		$this->bookId   = Paylog::get('id', 0);

		$bookObj = $this->bookHand->load($this->bookId);

		$name = $bookObj->getVar('name');
		$calenderOpen = $bookObj->getVar('calender_open');
		$summaryOpen  = $bookObj->getVar('summary_open');
		$detailOpen   = $bookObj->getVar('detail_open');

		$this->bookObj = $bookObj;

		$name = Paylog::post('name', $name);
		$calenderOpen = Paylog::post('calender_open', $calenderOpen);
		$summaryOpen  = Paylog::post('summary_open', $summaryOpen);
		$detailOpen   = Paylog::post('detail_open', $detailOpen);

		$this->input = array(
			'name'          => $name,
			'calender_open' => $calenderOpen,
			'summary_open'  => $summaryOpen,
			'detail_open'   => $detailOpen,
		);

		$this->openOptions = array(
			Paylog_Object_Book::OPEN_PUBLIC  => Paylog::msg("Public"),
			Paylog_Object_Book::OPEN_USERS   => Paylog::msg("Only Users"),
			Paylog_Object_Book::OPEN_PRIVETE => Paylog::msg("Private"),
		);
	}

	public function main()
	{
		$this->_validateAccess();

		try
		{
			if ( Paylog::post('save') )
			{
				$this->_save();
			}
		}
		catch(Exception $e)
		{
		}
		
		$this->_default();
	}

	protected function _validateAccess()
	{
		if ( !Paylog::$cms->isUser() )
		{
			Paylog::redirect("Please Login");
		}

		if ( $this->bookId )
		{
			if ( !$this->relBookUserHand->isOwner(Paylog::$cms->userId) )
			{
				Paylog::redirect("Only owner can edit.");
			}
		}
	}

	protected function _default()
	{
		$this->output['open_options'] = $this->openOptions;
		$this->output['input']  = $this->input;
		$this->output['token']  = Paylog_Class_Ticket::issue();
		$this->output['errors'] = $this->errors;

		$this->_view();
	}

	protected function _save()
	{
		$this->_validateInput();

		$this->bookObj->setVar('name', $this->input['name']);
		$this->bookObj->setVar('calender_open', $this->input['calender_open']);
		$this->bookObj->setVar('summary_open', $this->input['summary_open']);
		$this->bookObj->setVar('detail_open', $this->input['detail_open']);

		if ( !$this->bookHand->save($this->bookObj) )
		{
			$this->_addError("Book updating failed.");
			$this->_riseError();
		}

		$bookId = $this->bookObj->getVar('id');
		$userId = Paylog::$cms->userId;

		$relBookUserObj = $this->relBookUserHand->create();
		$relBookUserObj->setVar('book_id', $bookId);
		$relBookUserObj->setVar('uid', $userId);

		if ( !$this->relBookUserHand->save($relBookUserObj) )
		{
		}

		Paylog::redirect("Book successly updated.");
	}

	protected function _validateInput()
	{
		if ( !$this->input['name'] )
		{
			$this->_addError("Book name is blank.");
		}

		$calenderOpen = $this->input['calender_open'];
		if ( !isset($this->openOptions[$calenderOpen]) )
		{
			$this->_addError("Calender open value is not in the options.");
		}

		$summaryOpen = $this->input['summary_open'];
		if ( !isset($this->openOptions[$summaryOpen]) )
		{
			$this->_addError("Summary open value is not in the options.");
		}

		$detailOpen = $this->input['detail_open'];
		if ( !isset($this->openOptions[$detailOpen]) )
		{
			$this->_addError("Detail open value is not in the options.");
		}

		$token = Paylog::post('token');
		if ( !Paylog_Class_Ticket::check($token) )
		{
			$this->_addError("Ticket Error");
		}

		if ( count($this->errors) > 0 )
		{
			$this->_riseError();
		}
	}

	protected function _addError($msg)
	{
		$this->errors[] = Paylog::msg($msg);
	}

	protected function _riseError()
	{
		throw new Exception();
	}
}

?>
